<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="线索" prop="clueName">
        <el-input
          v-model="queryParams.clueName"
          placeholder="请输入线索"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入客户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="merchantsrecordList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="线索主题" align="center" prop="crmCustomerMerchants.clueName"/>
      <el-table-column label="客户信息" align="center" prop="crmCustomerMerchants.companyName"/>
      <el-table-column label="意向度" align="center" prop="intentionState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.merchantsrecord_type" :value="scope.row.intentionState"/>
        </template>
      </el-table-column>
      <el-table-column label="跟进方式" align="center" prop="investigationMode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.merchantsrecord_method" :value="scope.row.investigationMode"/>
        </template>
      </el-table-column>
      <el-table-column label="跟进记录" align="center" prop="record" width="300"/>
      <el-table-column label="跟进人" align="center" prop="userId"/>
      <el-table-column label="跟进时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import {
    listMerchantsrecord,
    getMerchantsrecord,
    delMerchantsrecord,
    addMerchantsrecord,
    updateMerchantsrecord
  } from "@/api/merchantsrecord/merchantsrecord";

  export default {
    name: "Merchantsrecord",
    dicts: ['merchantsrecord_type', 'merchantsrecord_method'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户线索跟进记录表格数据
        merchantsrecordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merchantsId: null,
          record: null,
          createTime: null,
          imgs: null,
          userId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询客户线索跟进记录列表 */
      getList() {
        this.loading = true;
        listMerchantsrecord(this.queryParams).then(response => {
          this.merchantsrecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          merchantsId: null,
          record: null,
          createTime: null,
          imgs: null,
          userId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加客户线索跟进记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getMerchantsrecord(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户线索跟进记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateMerchantsrecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMerchantsrecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除客户线索跟进记录编号为"' + ids + '"的数据项？').then(function () {
          return delMerchantsrecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('merchantsrecord/merchantsrecord/export', {
          ...this.queryParams
        }, `merchantsrecord_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
